package com.ccb.sc.common.modules.shopkeeper.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.modules.member.entity.Member;
import com.ccb.sc.common.modules.member.enums.MemberStatus;
import com.ccb.sc.common.modules.member.service.MemberService;
import com.ccb.sc.common.modules.shopkeeper.entity.Shopkeeper;
import com.ccb.sc.common.modules.shopkeeper.enums.ShopkeeperLevel;
import com.ccb.sc.common.modules.shopkeeper.mapper.ShopkeeperMapper;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperTeamListVo;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperTeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ken
 * @version 1.0
 * @date 2023/2/18 22:59
 */
@Service
public class ShopkeeperService extends ServiceImpl<ShopkeeperMapper, Shopkeeper> {

    @Autowired
    private MemberService memberService;

    public Shopkeeper findByMemberId(Long memberId) {
        return this.getOne(new QueryWrapper<Shopkeeper>().lambda().eq(Shopkeeper::getMemberId, memberId).last("limit 1"));
    }

    public PageVo<ShopkeeperTeamListVo> teamList(Long shopkeeperId, PageParams queryParam) {
        Page<Shopkeeper> page = new Page<>(queryParam.getCurrent(), queryParam.getPageSize());
        Page<ShopkeeperTeamListVo> resultPage = this.getBaseMapper().selectTeamList(page, shopkeeperId);
        for (ShopkeeperTeamListVo record : resultPage.getRecords()) {
            record.setPhone(StrUtil.hide(record.getPhone(), 3, 7));
        }
        return PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), resultPage.getTotal(), resultPage.getRecords());
    }

    public Shopkeeper create(Long memberId) {
        Shopkeeper shopkeeper = new Shopkeeper();
        shopkeeper.setMemberId(memberId);
        shopkeeper.setPoints(BigDecimal.ZERO);
        shopkeeper.setTotalPoints(BigDecimal.ZERO);
        shopkeeper.setLevel(ShopkeeperLevel.ORDINARY_MEMBER);
        shopkeeper.setWechatNo("");
        shopkeeper.setAssistantWechatNo("");
        shopkeeper.setStatus(MemberStatus.AVAILABLE);
        this.save(shopkeeper);
        return shopkeeper;
    }

    public ShopkeeperTeamVo team(Long shopkeeperId) {
        ShopkeeperTeamVo vo = new ShopkeeperTeamVo();
        Shopkeeper shopkeeper = this.getById(shopkeeperId);
        long count = this.memberService.count(Wrappers.<Member>lambdaQuery().eq(Member::getInviteId, shopkeeper.getMemberId()));
        vo.setInvites((int) count);
        List<Long> members = this.getBaseMapper().selectAllChildId(shopkeeper.getMemberId());
        vo.setTeams(members.size());
        shopkeeper.setTeam(members.size());
        if (members.size() > 0) {
            BigDecimal amount = this.getBaseMapper().selectAllAmount(members);
            vo.setAmount(amount);
            shopkeeper.setTotalAmount(amount);
        }else {
            vo.setAmount(BigDecimal.ZERO);
        }
        this.updateById(shopkeeper);
        return vo;
    }
}
